package javat.util;

import static com.google.common.collect.Maps.newHashMap;
import static net.ftng.commons.lang.utils.PrintUtils.println;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.ftng.commons.lang.Monitor;
import net.ftng.commons.lang.utils.MonitorUtils;

import org.junit.Test;

public class MapTest {

    Map<Long, Long> map;

    @Test
    public void nullKey() {

        map = new HashMap<Long, Long>();
        map.put(null, 1L);
        println(map);
        // null can be a key
    }

    @Test
    public void values_allValueIsNull() {

        map = new HashMap<Long, Long>();
        map.put(1L, null);
        map.put(2L, null);
        map.put(3L, null);
        Collection<Long> values = map.values();
        assertEquals(3, values.size());
        for (Long value : values) {
            assertTrue(value == null);
        }
    }

    @Test
    public void capacity() {

        int dataSize = 300000;
        Monitor monitor = MonitorUtils.getMonitor(this.getClass().getSimpleName(), "capacity");
        Map<Long, Long> map1 = newHashMap();
        monitor.start();
        for (long i = 0; i < dataSize; i++) {

            map1.put(i, i);
        }
        monitor.stopAndprintResult("Not given capacity");

        Map<Long, Long> map2 = new HashMap<Long, Long>(dataSize);
        monitor.start();
        for (long i = 0; i < dataSize; i++) {

            map2.put(i, i);
        }
        monitor.stopAndprintResult("Given %d capacity", dataSize);
    }

    @Test
    public void removeKeyWhenIteratitor() {

        Map<Object, Object> testMap = newHashMap();
        testMap.put(1, 1);
        testMap.put(2, 1);
        testMap.put(3, 1);
        println(testMap);

        Iterator<Entry<Object, Object>> iterator = testMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<Object, Object> entry = iterator.next();
            println(entry);
            iterator.remove();
        }
        println(testMap);
    }
}
